/*
 * Copyright 2009-2010 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.text.actions;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;

import org.kommando.core.action.ActionExecutionException;
import org.kommando.core.catalog.CatalogObject;
import org.kommando.text.actions.support.TextAction;

/**
 * @author Peter De Bruycker
 */
public class CreateFileAction extends TextAction {

    private static final String ID = "text:createFile";
    private Icon icon;

    @Override
    protected CatalogObject execute(String text, CatalogObject indirect) throws ActionExecutionException {
        // TODO save the current directory between sessions

        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        // fileChooser.setAcceptAllFileFilterUsed(true);
        // fileChooser.setFileFilter(new FileFilter() {
        // @Override
        // public String getDescription() {
        // return "Text Documents (*.txt)";
        // }
        //        
        // @Override
        // public boolean accept(File f) {
        // return true;
        // }
        // });
        int result = fileChooser.showSaveDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            if (selectedFile.exists()) {
                // ask user for confirmation
            }
            try {
                Writer writer = new FileWriter(selectedFile);
                writer.write(text);
                writer.close();
            } catch (IOException e) {
                // TODO handle exception
                throw new RuntimeException(e);
            }
        }

        return null;
    }

    @Override
    public String getDescription() {
        return "Create a file containing the text";
    }

    @Override
    public Icon getIcon() {
        if (icon == null) {
            icon = new ImageIcon(getClass().getResource("/icons/document-save-as_256x256x32.png"));
        }

        return icon;
    }

    @Override
    public String getName() {
        return "Create File...";
    }

    @Override
    public String getId() {
        return ID;
    }

}
